﻿using AutoMapper;
using BaseData.API.Read.Applications.Commands;
using BaseData.API.Read.Dtos;
using BaseData.Domain;
using BaseData.ErrorCode;
using BaseData.Infrastructure.Interfaces;
using MediatR;

namespace BaseData.API.Read.Applications.CommandHandlers
{
    /// <summary>
    /// 管理用户配置功能显示 命令
    /// </summary>
    public class UserByMenuQueryCommandHandler : IRequestHandler<UserByMenuQueryCommand, ApiResult<ApiPaging<List<UserByMenuDTO>>>>
    {
        private readonly IBaseRepository<User> userRep;
        private readonly IBaseRepository<UserRoleRelation> userRoleRep;
        private readonly IBaseRepository<Role> roleRep;
        private readonly IBaseRepository<UserMenuPermissionRelation> userMenuRep;
        private readonly IBaseRepository<MenuPermission> menuPermissionRep;
        private readonly IMapper mapper;
        private readonly ILogger<UserByMenuQueryCommandHandler> logger;

        public UserByMenuQueryCommandHandler(IBaseRepository<User> userRep,
            IBaseRepository<UserRoleRelation> userRoleRep,
            IBaseRepository<Role> roleRep,
            IBaseRepository<UserMenuPermissionRelation> userMenuRep,
            IBaseRepository<MenuPermission> menuPermissionRep,
            IMapper mapper,
            ILogger<UserByMenuQueryCommandHandler> logger)
        {
            this.userRep = userRep;
            this.userRoleRep = userRoleRep;
            this.roleRep = roleRep;
            this.userMenuRep = userMenuRep;
            this.menuPermissionRep = menuPermissionRep;
            this.mapper = mapper;
            this.logger = logger;
        }
        public Task<ApiResult<ApiPaging<List<UserByMenuDTO>>>> Handle(UserByMenuQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<List<UserByMenuDTO>>> result = new ApiResult<ApiPaging<List<UserByMenuDTO>>>();
            result.Code = ApiEnum.查询成功;
            result.Message = ApiEnum.查询成功.ToString();

            var query = userRep.GetAll().Where(x => !x.IsDel);
            var querys = roleRep.GetAll().Where(x => x.IsDel);
            if (!string.IsNullOrEmpty(request.UserNickname))
            {
                query = query.Where(x => x.UserNickname.Contains(request.UserNickname));
            }
            if (!string.IsNullOrEmpty(request.RoleName))
            {
                querys = querys.Where(x => x.RoleName.Contains(request.RoleName));
            }
            //分页
            int TotalCount = query.Count();
            int PageCount = (int)Math.Ceiling((double)TotalCount / request.PageSize);

            var list = query.OrderBy(x => x.UserId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();

            var data = mapper.Map<List<UserByMenuDTO>>(list);
            foreach (var item in data)
            {
                var roleIds = userRoleRep.GetAll().Where(x => x.UserId == item.UserId).Select(x => x.RoleId).ToList();
                var roleNames = roleRep.GetAll().Where(x => roleIds.Contains(x.RoleId)).Select(x => x.RoleName).ToList();
                item.RoleId = roleIds;
                item.RoleName = roleNames;

                var menuIds=userMenuRep.GetAll().Where(x=>x.UserId==item.UserId).Select(x=>x.MenuId).ToList();
                var menuNames=menuPermissionRep.GetAll().Where(x=>menuIds.Contains(x.MenuId)).Select(x=>x.MenuName).ToList();
                item.MenuId = menuIds;
                item.MenuName = menuNames;
            }
            ApiPaging<List<UserByMenuDTO>> paging = new ApiPaging<List<UserByMenuDTO>>();
            paging.TotalCount = TotalCount;
            paging.PageCount = PageCount;
            paging.Data = data;

            result.Data = paging;

            return Task.FromResult(result);
        }
    }
}
